<template>
  <div class="stateWait">
    <div class="searchDiv">
      <Form :model="searchItem" :label-width="90">
        <Row :gutter="10">
          <Col span="6">
                    <FormItem label="电站">
                        <Select v-model="searchItem.powerStation" clearable filterable @on-change="getHd">
                   <Option v-for="(item, index) in powerStationList" :value="item" :key="index">{{item}}</Option>
               </Select>
                    </FormItem>
           </Col>
           <Col span="6">
            <FormItem label="HD编号">

                 <Select v-model="searchItem.hdNumber" clearable filterable :disabled="HBnumberList.length==0" @on-change="getJz">
                  <Option v-for="(item, index) in HBnumberList" :value="item" :key="index">{{item}}</Option>
              </Select>
            </FormItem>
          </Col>
          <Col span="6">
            <FormItem label="机组编号">
             <Select v-model="searchItem.aircrewNumber" clearable filterable :disabled="JZnumberList.length==0">
                  <Option v-for="(item, index) in JZnumberList" :value="item" :key="index">{{item}}</Option>
              </Select>
            </FormItem>
          </Col>
          <Col span="6">
                    <FormItem label="类型">
                      <Select v-model="searchItem.alarmAlias" clearable filterable>
                          <!-- <Option v-for="(item, index) in LeibieList" :value="item.displayName" :key="index">{{item.displayName}}</Option> -->
                          <Option value="开机">开机</Option>
                          <Option value="停机">停机</Option>
                      </Select>
                    </FormItem>
           </Col>
          <Col span="6">
            <FormItem label="状态变更时刻">
              <DatePicker
                type="datetimerange"
                format="yyyy-MM-dd HH:mm:ss"
                @on-change="handleChangeSearchClock"
                placeholder="选择日期和时间"
                style="width: 100%"
              ></DatePicker>
            </FormItem>
          </Col>
          <Col span="6">
            <Button type="primary" @click="searchItem.pageNo=1;search()" class="marRight">查询</Button>
          </Col>
        </Row>
      </Form>
    </div>
    <Table :loading="loading" border :columns="columns" :data="tableData"></Table>
    <Page show-total :total="total" show-elevator :page-size='searchItem.pageSize' :current="searchItem.pageNo" @on-change='pageChange'  style="margin:20px auto;text-align:center"/>

    <!-- 新增弹窗开始 -->
    <Modal :mask-closable="false" v-model="modal1" title="编辑机组状态待办" width="1000" >
      <div class="baseTitle">基本信息</div>
           <Form :label-width="100">
             <Row :gutter="10" class="xzjz">
                 <Col span="6">
                    <FormItem :model="formItem" label="电站：">
                     {{formItem.powerStation}}
                    </FormItem>
                  </Col>
                  <Col span="6">
                    <FormItem label="HD编号：">
                     {{formItem.hdNumber}}
                    </FormItem>
                  </Col>
                  <Col span="6">
                    <FormItem label="机组编号：">
                    {{formItem.aircrewNumber}}
                    </FormItem>
                  </Col>
             </Row>
           </Form>
           <div class="baseTitle">状态信息</div>
           <Form :label-width="100">
             <Row :gutter="10" class="xzjz">
                 <Col span="6">
                    <FormItem label="类型：">
                        <span v-if="formItem.alarmAlias=='正常'||formItem.alarmAlias=='危险'||formItem.alarmAlias=='报警'">开机</span>
                        <span v-if="formItem.alarmAlias=='停机'||formItem.alarmAlias=='停车'||formItem.alarmAlias=='断网'">停机</span>
                    </FormItem>
                  </Col>
                  <Col span="6">
                    <FormItem label="机组状态：">
                      <!-- <Select v-model="formItem.status">
                        <Option value="全部">全部</Option>
                        <Option value="测试">测试</Option>
                     </Select> -->

                      <Select v-model="formItem.statusDictId" clearable filterable @on-change="getSelect2">
                          <Option v-for="(item, index) in JZstatusList3" :value="item.id" :key="index">{{item.displayName}}</Option>
                      </Select>
                    </FormItem>
                  </Col>
                  <Col span="6">
                    <FormItem label="停机时刻：">
                     {{formItem.statusUpdateTime}}
                    </FormItem>
                  </Col>
                  <Col span="6">
                    <FormItem label="累计运行时间：">
                      {{(formItem.runningTime/(1000*3600)).toFixed(2) }} 小时
                    </FormItem>
                  </Col>
               </Row>
           </Form>
           <Form :label-width="100">
             <Row :gutter="10" class="xzjz">
                  <div class="baseTitle">填报信息</div>
                  <Col span="6">
                    <FormItem label="记录人：">
                      <!-- <Input v-model="formItem.recordName" placeholder="请输入"></Input> -->
                      <Input v-model="recordName" disabled placeholder="请输入"></Input>
                    </FormItem>
                  </Col>
                  <Col span="6">
                    <FormItem label="记录时间：">
                      <!-- <DatePicker
                        type="datetime"
                        v-model="formItem.recordTime"
                        placeholder="记录时间"
                        style="width: 100%"
                      ></DatePicker> -->
                       <Input v-model="recordTime" disabled placeholder="请输入"></Input>
                    </FormItem>
                  </Col>
             </Row>
           </Form>
           <div slot="footer">
               <Button @click="modal1=false">取消</Button>
              <Button type="primary" :loading="btnClick" @click="ok">确定</Button>
          </div>
    </Modal>
    <!-- 新增弹窗结束 -->
  </div>
</template>

<script>

import {
  getSelectOption,
  addAircrewStatusRecord,
  getDaiBan,
  removeAircrewStatusRecord,
  updateAircrewStatusRecord
} from '@/api/runManage'
import {getTime,getAllEqu
} from '@/api/select'
export default {
  data () {
    return {
      btnClick:false,
      recordName: localStorage.getItem('recordUserName'),
        recordId:localStorage.getItem('recordUserId'),
        recordTime:getTime(),
        loading:true,
      total: 0,
      modal1: false,
      loading1: false,
      powerStationList: '',
      loading2: false,
      HBnumberList: '',
      loading3: false,
      allEquList:[],
      allEquList2:[],
      JZnumberList: '',
       JZnumberList:[],
      JZstatusList:[],
      JZstatusList2:[],
      JZstatusList3:[],
      searchItem: {
        pageNo: 1,
        pageSize: 10
      },
      formItem: {

      },
      columns: [
        {
          type: 'index',
          width: 60,
          title: '序号',
          align: 'center'
        },
        {
          title: '电站',
          key: 'powerStation'
        },
        {
          title: 'HD编号',
          key: 'hdNumber'
        },
        {
          title: '机组编号',
          key: 'aircrewNumber'
        },
        {
          title: '类型',
          key: 'alarmAlias',
          render: (h, params) => {
            let state=''
            if(params.row.alarmAlias=='正常'||params.row.alarmAlias=='危险'||params.row.alarmAlias=='报警'){
              state='开机'
            } else {
              state='停机'
            }
            return h('div', state)
          }
        },
        // {
        //   title: '机组状态',
        //   key: 'statusDisplayName'
        // },
        {
          title: '状态变更时刻',
          key: 'statusUpdateTime'
        },
        {
          title: '累计运行时间(小时)',
          key: 'runningTime',
          width: 150,
          align: 'center',
          render: (h, params) => {
            return h('div',(params.row.runningTime/(1000*3600)).toFixed(2)+'小时')
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 100,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.edit(params)
                    }
                  }
                },
                '处理'
              )
            ])
          }
        }
      ],
      tableData: []
    }
  },
  methods: {
    getSelect2(e){
       var arry=this.JZstatusList3.filter(item => item.id==e)
       this.formItem.statusDisplayName=arry[0].displayName
       this.formItem.statusValue=arry[0].value
    },
    pageChange (e) {
      this.searchItem.pageNo = e
      console.log(this.searchItem.pageNo, '页码')
      this.search()
    },
    handleChangeSearchClock (e) {
      this.searchItem.startStatusUpdateTime = e[0];
      this.searchItem.endStatusUpdateTime = e[1];
    },
    // 获取电站类型列表
    // powerStation () {
    //   getSelectOption(
    //     {
    //       code: 'dianzhan'
    //     }
    //   ).then(res => {
    //     this.powerStationList = res.data
    //   }).catch(err => {
    //     console.error('err>' + JSON.stringify(err))
    //   })
    // },
    // HBnumber () {
    //   getSelectOption(
    //     {
    //       code: 'hd_code'
    //     }
    //   ).then(res => {
    //     this.HBnumberList = res.data
    //   }).catch(err => {
    //     console.error('err>' + JSON.stringify(err))
    //   })
    // },
    // JZnumber () {
    //   getSelectOption(
    //     {
    //       code: 'jizu_code'
    //     }
    //   ).then(res => {
    //     this.JZnumberList = res.data
    //   }).catch(err => {
    //     console.error('err>' + JSON.stringify(err))
    //   })
    // },
     JZstatus () {
      getSelectOption(
        {
          code: 'jizhu_status'
        }
      ).then(res => {
        this.JZstatusList = res.data
      }).catch(err => {
        console.error('err>' + JSON.stringify(err))
      })
      getSelectOption(
        {
          code: 'jizhu_status2'
        }
      ).then(res => {
        this.JZstatusList2 = res.data
      }).catch(err => {
        console.error('err>' + JSON.stringify(err))
      })
    },
    Leibie () {
      getSelectOption(
        {
          code: 'catory'
        }
      ).then(res => {
        this.LeibieList = res.data
      }).catch(err => {
        console.error('err>' + JSON.stringify(err))
      })
    },
    edit (params) {
      this.formItem = params.row;
      if(this.formItem.alarmAlias=='正常'||this.formItem.alarmAlias=='危险'||this.formItem.alarmAlias=='报警'){
        //开机
        this.JZstatusList3=this.JZstatusList
      } else{
        //停机
        this.JZstatusList3=this.JZstatusList2
      }

      this.modal1 = true;
    },
    ok () {
      this.formItem.recordName=this.recordName;
      this.formItem.recordId=this.recordId;
      this.formItem.recordTime=this.recordTime;
      this.btnClick=true;
      updateAircrewStatusRecord(this.formItem).then(res => {

        if (res.code == 0) {
          this.$Message.info('修改成功')
          this.search()
          this.modal1=false;
          this.btnClick=false;
        } else {
          this.$Message.info(res.message)
          this.modal1=false;
          this.btnClick=false;
        }
      }).catch(err => {
        console.error('err>' + JSON.stringify(err))
      })
    },

    search () {
      console.log(this.searchItem)
      getDaiBan(this.searchItem).then(res => {
        console.log('get search:', res)
        if (res.code == 0) {
          this.tableData = res.data.list
          this.total = res.data.total
          this.loading=false;
        } else {
          this.$Message.info(res.message)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    getAllEqument(){
     getAllEqu(null).then(res => {
        this.allEquList=res.data;
        let arr = res.data.map(function(item) {
              return item.powerStation;
        });
        this.powerStationList=[...new Set(arr)]
      })
    },
    getHd(e){
      this.JZnumberList=[];
      this.allEquList2=this.allEquList.filter(
        item =>item.powerStation == e
      );
      let arr2=this.allEquList2.map(function(item) {
        return item.hdNumber;
      });
      this.HBnumberList=[...new Set(arr2)];
    },
    getJz(e){
      let arr=this.allEquList2.filter(
        item =>item.hdNumber == e
      );
      let arr2=arr.map(function(item) {
        return item.aircrewNumber;
      });
      this.JZnumberList=[...new Set(arr2)];
    }
  },
  mounted () {
   this.getAllEqument()
     this.JZstatus();
    this.Leibie();
    this.search()
  }
}
</script>

<style lang="less">
.stateWait {
  background: #fff;
  height: 100%;
  padding: 15px;
  font-size: 14px;
  .searchDiv {
    .marRight {
      margin-right: 15px;
    }
  }
  .ivu-form-item {
    margin-bottom: 15px;
  }
}
.baseTitle {
    font-weight: bold;
    font-size:16px;
    margin-bottom:10px;
  }
 .xzjz  {
   .ivu-form-item {
    margin-bottom: 10px;
  }
 }
</style>
